package com.openthree.common.utils;//package xin.cymall.utils;
//
//import org.activiti.engine.*;
//import org.activiti.engine.history.HistoricTaskInstance;
//import org.activiti.engine.impl.persistence.entity.ProcessDefinitionEntity;
//import org.activiti.engine.impl.pvm.PvmActivity;
//import org.activiti.engine.impl.pvm.PvmTransition;
//import org.activiti.engine.impl.pvm.process.ActivityImpl;
//import org.activiti.engine.impl.pvm.process.ProcessDefinitionImpl;
//import org.activiti.engine.impl.pvm.process.TransitionImpl;
//import org.activiti.engine.runtime.ProcessInstance;
//import org.activiti.engine.task.Task;
//
//import java.util.ArrayList;
//import java.util.List;
//import java.util.Map;
//
///**
// * Created by mhj
// * 2017/10/20.
// */
//public class ActivitiUtil {
//
//    public static ProcessEngine getEngine(){
//        ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//        return processEngine;
//    }
//
//    private static ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
//    private static IdentityService identityService = processEngine.getIdentityService();
//    private static RuntimeService runtimeService = processEngine.getRuntimeService();
//    private static TaskService taskService = processEngine.getTaskService();
//    private static RepositoryService repositoryService = processEngine.getRepositoryService();
//    private static HistoryService historyService = processEngine.getHistoryService();
//    public static void TaskRollBack(String taskId){
//        try {
//            Map<String, Object> variables;
//            // 取得当前任务
//            HistoricTaskInstance currTask = historyService
//                    .createHistoricTaskInstanceQuery().taskId(taskId)
//                    .singleResult();
//            // 取得流程实例
//            ProcessInstance instance = runtimeService
//                    .createProcessInstanceQuery()
//                    .processInstanceId(currTask.getProcessInstanceId())
//                    .singleResult();
//            if (instance == null) {
//
//                //流程结束
//            }
//            variables = instance.getProcessVariables();
//            // 取得流程定义
//            ProcessDefinitionEntity definition = (ProcessDefinitionEntity) (processEngine.getRepositoryService().getProcessDefinition(currTask
//                    .getProcessDefinitionId()));
//
//            if (definition == null) {
//
//                //log.error("流程定义未找到");
//                return ;
//            }
//            // 取得上一步活动
//            ActivityImpl currActivity = ((ProcessDefinitionImpl) definition)
//                    .findActivity(currTask.getTaskDefinitionKey());
//            List<PvmTransition> nextTransitionList = currActivity
//                    .getIncomingTransitions();
//            // 清除当前活动的出口
//            List<PvmTransition> oriPvmTransitionList = new ArrayList<PvmTransition>();
//            List<PvmTransition> pvmTransitionList = currActivity
//                    .getOutgoingTransitions();
//            for (PvmTransition pvmTransition : pvmTransitionList) {
//                oriPvmTransitionList.add(pvmTransition);
//            }
//            pvmTransitionList.clear();
//
//            // 建立新出口
//            List<TransitionImpl> newTransitions = new ArrayList<TransitionImpl>();
//            for (PvmTransition nextTransition : nextTransitionList) {
//                PvmActivity nextActivity = nextTransition.getSource();
//                ActivityImpl nextActivityImpl = ((ProcessDefinitionImpl) definition)
//                        .findActivity(nextActivity.getId());
//                TransitionImpl newTransition = currActivity
//                        .createOutgoingTransition();
//                newTransition.setDestination(nextActivityImpl);
//                newTransitions.add(newTransition);
//            }
//            // 完成任务
//            List<Task> tasks = taskService.createTaskQuery()
//                    .processInstanceId(instance.getId())
//                    .taskDefinitionKey(currTask.getTaskDefinitionKey()).list();
//            for (Task task : tasks) {
//                taskService.complete(task.getId(), variables);
//                historyService.deleteHistoricTaskInstance(task.getId());
//            }
//            // 恢复方向
//            for (TransitionImpl transitionImpl : newTransitions) {
//                currActivity.getOutgoingTransitions().remove(transitionImpl);
//            }
//            for (PvmTransition pvmTransition : oriPvmTransitionList) {
//                pvmTransitionList.add(pvmTransition);
//            }
//
//
//            return ;
//        } catch (Exception e) {
//
//            return ;
//        }
//    }
//}
